package com.zj.demo01.controller;

import com.github.pagehelper.PageInfo;
import com.zj.demo01.dto.AirDTO;
import com.zj.demo01.entity.Air;
import com.zj.demo01.entity.District;
import com.zj.demo01.service.AirService;
import com.zj.demo01.vo.ResultVO;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:AirStarterApp
 * @author:[开发者名称]
 * @Description:[类的功能描述]
 * @Date:2025/9/3/12:50
 * @Version:1.0
 */
@RestController
public class AirController {
    @Autowired
    private AirService airService;

    /**
     * 查询所有数据
     *
     * @return
     */

    //GET	http://localhost:8080/district/list
    @GetMapping("/district/list")
    public ResultVO districtList() {
        //查询库中表diatrict所有信息
        List<District> list = airService.districtList();
        //封装返回结果


        ResultVO resultVO = new ResultVO();
        resultVO.setCode(0);
        resultVO.setMsg("1");
        resultVO.setData(list);
        return resultVO;
    }

    /**
     * 分页查询+条件查询
     */
    @GetMapping("/air/list")
    public ResultVO airList(@RequestParam(defaultValue = "1")  Integer page ,
                            @RequestParam(defaultValue = "5") Integer size,
                            Integer districtID) {
        PageInfo pageInfo =airService.findAirBydistrictIdAndPage(page,size,districtID);

        ResultVO resultVO = new ResultVO();
        resultVO.setCode(0);
        resultVO.setMsg("1");
        resultVO.setData(pageInfo);
        return resultVO;
    }
    @PostMapping("/air/add")
    public ResultVO airAdd(@Valid AirDTO airDTO, BindingResult result){
        ResultVO resultVO = new ResultVO();
        //查看是否合法参数
        if (result.hasErrors()){
            String message = result.getFieldError().getDefaultMessage();
            resultVO.setCode(400);
            resultVO.setMsg(message);
         return resultVO;
        }

        airService.airAdd(airDTO);

        resultVO.setCode(0);
        resultVO.setMsg("1");
        return resultVO;
    }
    @PutMapping()
    public ResultVO airUpdate(Air air){
        ResultVO resultVO = new ResultVO();
        Integer id = air.getId();
        if (id==null){
            resultVO.setCode(400);
            resultVO.setMsg("参数不合法");
            return resultVO;
        }

        int count  = airService.updateById(air);
        resultVO.setCode(0);
        resultVO.setMsg("1");
        return resultVO;

    }
    @DeleteMapping()
    public ResultVO deleteByID(Integer ID){
        ResultVO resultVO = new ResultVO();

        airService.deleteByID(ID);
        resultVO.setCode(0);
        resultVO.setMsg("1");
        return resultVO;
    }


}
